 module.exports ={
    chat: function() {
        this.cmdsHandlers = {
                'HELP':{"mod":"chat","fn":"getHelp","onhelp":true,"help_message":"[/help commande] Afficher les commandes possible. Argument: 1=> nom de la commande (ex: /help quit), si pas de commande affiche toutes les commandes"},
                'CONU':{"mod":"chat","fn":"userConnect","onhelp":false,"help_message":""},
                'REGU':{"mod":"chat","fn":"userRegister","onhelp":false,"help_message":""},
                'SETU':{"mod":"chat","fn":"userSetRights","onhelp":false,"help_message":""},
                'DELU':{"mod":"chat","fn":"userDrop","onhelp":false,"help_message":""},
                'SGRP':{"mod":"chat","fn":"userSetGroup","onhelp":true,"help_message":"[/setgroup pub_id|pseudo group] Modifier le groupe de l'utilisateur. Arguments: 1=> pub_id ou pseudo de l'utilisateur, 2=> nom ou id du groupe"},
                'KIKU':{"mod":"chat","fn":"userKick","onhelp":true,"help_message":"[/kick pub_id|pseudo] Kicker un utilisateur. Argument: 1 => pub_id ou pseudo"},
                'BANU':{"mod":"chat","fn":"userBan","onhelp":true,"help_message":"[/ban pub_id|pseudo nb_jours] Bannir un utilisateur pour x jours, Arguments: 1=> pub_id ou pseudo a bannir, 2=> nombre de jours de ban (0 = ban a vie)"},
                'JOIU':{"mod":"chat","fn":"userJoin","onhelp":false,"help_message":""},
                'SEND':{"mod":"chat","fn":"sendMessage","onhelp":false,"help_message":""},
                'MESA':{"mod":"chat","fn":"meSay","onhelp":true,"help_message":"[/me texte] Affiche un texte style commentaire, ex: /me s'endort affichera a tous un message \"Jean s'endort\". Argument: 1=> Texte qui sera affiche apres votre pseudo"},
                'QUIT':{"mod":"chat","fn":"userQuit","onhelp":true,"help_message":"[/quit raison] Quitter le chat. Argument: 1=> texte pour indiquer la raison"},
                'INFO':{"mod":"chat","fn":"getStatus","onhelp":false,"help_message":""},
                'ULST':{"mod":"chat","fn":"usersList","onhelp":false,"help_message":""},
                'PMSG':{"mod":"chat","fn":"userPM","onhelp":true,"help_message":"[/mp pub_id|pseudo texte] Envoi un message priv&eacute; a un utilisateur. Arguments: 1=> pub_id ou pseudo de la personne, 2=> Texte que vous souhaitez lui envoyer"}
            };
        
        /**
         * Init
         */
        this.init = function(){
            main.dispatcher.registerHook(this.cmdsHandlers);
        };
        
        
        /**
         * Commands
         */
        
        //help
        this.getHelp = function(data){
            
        };
        
        //basics
        this.sendMessage = function(data){
            if (data.TEXT != null && data.PUB_ID != null) {
                var json = {
                    "MESG" : {
                        "TEXT" : data.TEXT,
                        "FROM" : main.dispatcher.getMod('userslist').getUserProperty(data.PUB_ID, "nickname"),
                    }
                };
                main.websockman.sendToAll(JSON.stringify(json));
            }
            
        };
        
        this.meSay = function(data){
            
        };
        
        this.getStatus = function(data){
            
        };
        
        this.usersList = function(data){
            
        };
        
        
        //user
        this.userConnect = function(data){
            if (data.EMAIL != undefined && data.EMAIL != "" && data.PUB_ID != "") {
                var pubID = data.PUB_ID;
                if (data.PASSWORD != undefined) {
                    var query = main.db.query('SELECT * FROM chat_users WHERE email="'+data.EMAIL+'"');
                    query.on('result', function(results, data){
                        console.log(results);
                        console.log(data);
                        for (index in results ){
                            if (results[index].password == data.PASSWORD) {
                                if(main.dispatcher.modExist("userslist")){
                                    main.dispatcher.getMod('userslist').addUser(data.PUB_ID, data, results);
                                }
                                var json = {
                                    "CONN": {
                                        "REPLY" : "OK"
                                        }
                                    };
                                main.websockman.send(pubID, JSON.stringify(json));
                                var json = {
                                    "UINF" : {
                                        "TEXT" : data.NICKNAME+" s'est connect&eacute;"
                                        }
                                    };
                                main.websockman.sendToAll(JSON.stringify(json));
                            }else{
                                var json = {
                                        "CONN": {
                                            "REPLY" : "WPWD"
                                            }
                                        };
                                main.websockman.send(pubID, JSON.stringify(json));
                            }
                        }
                    });
                }else{
                    if(main.dispatcher.modExist("userslist")){
                        main.dispatcher.getMod('userslist').addUser(data.PUB_ID, data, false);
                    }
                    var json = {
                        "CONN": {
                            "REPLY" : "OK"
                            }
                        };
                    main.websockman.send(pubID, JSON.stringify(json));
                    var json = {
                        "UINF" : {
                            "TEXT" : data.NICKNAME+" s'est connect&eacute;"
                            }
                        };
                    main.websockman.sendToAll(JSON.stringify(json));
                }
            }else{
                var json = {
                    "CONN": {
                        "REPLY" : "NOK"
                        }
                    };
                main.websockman.send(pubID, JSON.stringify(json));
            }
        };
        
        this.userRegister = function(data){
            
        };
        
        this.userSetRights = function(data){
            
        };
        
        this.userDrop = function(data){
            
        };
        
        this.userSetGroup = function(data){
            
        };
        
        this.userKick = function(data){
            
        };
        
        this.userBan = function(data){
            
        };
        
        this.userJoin = function(data){
            
        };
        
        this.userQuit = function(data){
            
        };
        
        this.userPM = function(data){
            
        };
        
        
        //chan
        this.openChan = function(data){
            
        };
        
        this.closeChan = function(data){
            
        };
    }
}